package com.example.controller;

import com.example.bean.Account;
import com.example.service.IAccountService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: my-spring-boot
 * @description: AccountController
 * @author: JackDong
 * @create: 2018-04-09 17:35
 **/
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    IAccountService accountService;
    
    /** 
    * 查询用户列表
    * @Param: [] 
    * @Return: java.util.List<com.example.bean.Account> 
    * @Author: JackDong.ch 
    * @Date: 2018/4/11 
    */ 
    @ApiOperation(value="获取用户列表", notes="获取用户列表")
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Account> getAccounts(){
        return accountService.findAccountList();
    }

    /**
    * 通过id查询账户信息
    * @Param: [id]
    * @Return: com.example.bean.Account
    * @Author: JackDong.ch
    * @Date: 2018/4/11
    */
    @ApiOperation(value="查询用户信息", notes="查询用户信息")
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public  Account getAccountById(@PathVariable("id") int id){
        return accountService.findAccountById(id);
    }

    /** 
    * 更新用户 
    * @Param: [id, name, money] 
    * @Return: java.lang.String 
    * @Author: JackDong.ch 
    * @Date: 2018/4/11 
    */ 
    @ApiOperation(value="更新用户", notes="根据url的id来指定更新用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long",paramType = "path"),
            @ApiImplicitParam(name = "name", value = "用户名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "money", value = "用户金额", required = true, dataType = "double")
    })
    //@RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public String updateAccount( @PathVariable("id")int id ,
                                 @RequestParam(value = "name",required = true)String name,
                                 @RequestParam(value = "money" ,required = true)double money){
        Account account = new Account();
        account.setMoney(money);
        account.setName(name);
        account.setId(id);
        int t=accountService.update(account);
        if(t==1){
            return account.toString();
        }else {
            return "fail";
        }
    }

    /** 
    * 创建用户
    * @Param: [name, money] 
    * @Return: java.lang.String 
    * @Author: JackDong.ch 
    * @Date: 2018/4/11 
    */ 
    @ApiOperation(value="创建用户", notes="创建用户")
    @RequestMapping(value = "",method = RequestMethod.POST)
    public  String postAccount( @RequestParam(value = "name")String name,
                                @RequestParam(value = "money" )double money){
        Account account=new Account();
        account.setMoney(money);
        account.setName(name);
        int t= accountService.add(account);
        if(t==1){
            return account.toString();
        }else {
            return "fail";
        }
    }
}
